package kevin.framework.basic.common.utils;

/**
 * @ClassName: DESBase64Util
 * @Description: DESBase64Util加密解密
 * @author kevin.huang
 * @date 2015年10月26日 下午4:57:59
 */
import java.io.IOException;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.codec.binary.Base64;

public class DESBase64Util {

    static String KEY_ALGORITHM_NAME = "DES";
    public static final String CIPHER_ALGORITHM_NAME = "DES";

    public static void main(String[] args) throws Exception {

        byte[] key = "DAF3188DCD39CAEB5DD35E9652KJH286".getBytes();
        byte[] encrypt = encrypt("888888".getBytes(), key);
        String encodeBase64String = Base64.encodeBase64String(encrypt);
        System.out.println(encodeBase64String);
        System.out.println(new String(decrypt(encrypt, key)));

    }

    /**
     * 生成秘钥key
     * 
     * @return
     * @throws Exception
     */
    private static byte[] generateKey() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM_NAME);
        keyGenerator.init(56); // des 必须是56, 此初始方法不必须调用
        SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    }

    /**
     * byte to key
     * 
     * @param key
     * @return
     * @throws Exception
     */
    private static Key toKey(byte[] key) throws Exception {
        DESKeySpec des = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM_NAME);
        SecretKey secretKey = keyFactory.generateSecret(des);
        return secretKey;
    }

    /**
     * 加密
     * 
     * @param data
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        Key k = toKey(key);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_NAME);
        cipher.init(Cipher.ENCRYPT_MODE, k);
        byte[] doFinal = cipher.doFinal(data);
        return cipher.doFinal(data);
    }

    /**
     * 解密
     * 
     * @param data
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        Key k = toKey(key);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_NAME);
        cipher.init(Cipher.DECRYPT_MODE, k);
        return cipher.doFinal(data);
    }

    /**
     * des 模式加密，采用默认的SysConfigParams.desKey
     * 
     * @param data
     * @return
     * @throws Exception
     */
    public static String desEncrypt(String data) throws Exception {
        return desEncrypt(data, SysConfigParams.desKey);
    }

    /**
     * des 模式加密
     * 
     * @param data
     * @param key
     * @return
     * @throws Exception
     */
    public static String desEncrypt(String data, String key) throws Exception {
        if (data == null || key == null) {
            return null;
        }
        byte[] bt = encrypt(data.getBytes(), key.getBytes());
        String encryptResultStr = Base64.encodeBase64String(bt);
        return encryptResultStr;
    }

    /**
     * des 模式解密，采用默认的SysConfigParams.desKey
     * 
     * @param data
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String desDecrypt(String data) throws IOException, Exception {
        return desDecrypt(data, SysConfigParams.desKey);
    }

    /**
     * des模式解密
     * 
     * @param data
     * @param key
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String desDecrypt(String data, String key) throws IOException, Exception {
        if (data == null || key == null) {
            return null;
        }
        byte[] buf = Base64.decodeBase64(data);
        byte[] bt = decrypt(buf, key.getBytes());
        return new String(bt);
    }

}
